//
//  VCInformationMoudle.m
//  VC
//
//  Created by 肖康龙 on 2017/8/30.
//  Copyright © 2017年 vsoontech. All rights reserved.


#import "VCInformationMoudle.h"
#import "VCCodeHelper.h"
#import <AFNetworking.h>

/*国内接口*/
#define URL_string @"http://res.entry.vsooncloud.com"

//#define URL_string @"http://p.entry.vsooncloud.com"

@implementation VCInformationMoudle

+ (AFHTTPSessionManager *)sharedHTTPSession{
    static AFHTTPSessionManager *manager ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.requestSerializer.timeoutInterval = 30;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"text/json",
                                                             @"text/javascript",
                                                             @"text/plain",
                                                             @"video/mpegurl",
                                                             @"application/octet-stream",
                                                             nil];
        AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        //是否可以使用自建证书（不花钱的）
        securityPolicy.allowInvalidCertificates=YES;
        //是否验证域名（一般不验证）
        securityPolicy.validatesDomainName=NO;
        
        [manager setSecurityPolicy:securityPolicy];
    });
    return manager;
}

/*获取秘钥*/
-(void)getKeyWithsucceed:(void(^)(id data))succeed fail:(void (^)(NSError *))fail{
    
    NSString  *urlstring = [NSString stringWithFormat:@"%@/v2/vcudp/kl",URL_string];

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[VCCodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
    
    [[VCInformationMoudle sharedHTTPSession] GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dictsss = [VCCodeHelper decryptionKeyWithString:responseObject];
        if (succeed) {
            succeed(dictsss);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
    }];

}


/*获取下载list
 *linkId    视频ID
 *seekVod   点播seek，毫秒
 */
-(void)getVideoListWithLinkId:(NSString *)linkId
                      seekVod:(NSInteger)seekVod
                      isStart:(BOOL)isStart
                      succeed:(void(^)(id data ))succeed
                         fail:(void(^)(NSError *error))fail{
//    NSString  *urlstring = @"http://res.test-entry.vsooncloud.com/v3/vcudp/resource?i=4FARfqdNynGUsB8KkcyCZXUNyp%2B3jdrTF3DZhUll7Q6J%2Fagm7WHu7dHecdEbkenxGJqt4uUZqQYagqKAW8mLNufYVk7xtovilxgF1YjmJsBeoWfPDqR9ofAthdwCeZKU5JkYr3iKAo5n6Rg%2BFi91Kk%2BWXNli%2BlxX2RSKBaUxW9yLJ%2FlZzVKMyv3y%2FgnUQ6LXjVPX9iDa5YXVO8r0j8S3sMiSzwFuUiAttw7NoXAKTHNK0DQI6isOKx2717K13s47Xt7ZgkPSjJ%2FZbk5vCn7fl8xveLty1XnefgEg1fuSM250SeuxaXFkPmPzCUO8XQzFLAzqOYLt8d4nVuLIIMKw1hsyfkNhDs1GNp014HXPbHI%3D&e=nu6UTKo09%2FswiZcCTIMDj7DNNiMPl8VS6sVcvZoeAZtbTfDxxgxLK69xGQr8nX0LvJ47OXilCwXoL%2FlQqPwnR%2FIWaPzq4JUNAotKFi%2B897bWwLRNZJ6mH4F23bXH16UoGKMYnsRjliyd7nSIF78C%2B92U3yuEMk3EGorI3cPZBubLdqUufxJ4a027%2FTQ5Z%2B9CCe4i9IfFXxqgeH62z9LXuA%3D%3D";
    NSString  *urlstring = [NSString stringWithFormat:@"%@/v3/vcudp/resource",URL_string];
//20200
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[VCCodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
//    [params setObject:[CodeHelper getSoucressListCodeID:linkId] forKey:@"e"];
    NSString *codeId = [NSString stringWithFormat:@"vc://%@",linkId];
    [params setObject:[VCCodeHelper getSoucressListCodeID:codeId
                                                    cnt:0
                                                  index:0
                                               platform:@"iOS"
                                                  start:isStart
                                                seekVod:seekVod
                                               seekLive:0
                                                  vcurl:codeId] forKey:@"e"];
    
    
    [[VCInformationMoudle sharedHTTPSession] GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dictsss = [VCCodeHelper decryptionPlayDataWithString:responseObject];
        if (succeed) {
            succeed(dictsss);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
    
}

/*加速接口*/
-(NSURLSessionDataTask *)getAccelerateWithLinkId:(NSString *)linkId
                     urlString:(NSString *)urlString
                       succeed:(void(^)(id data ))succeed
                          fail:(void(^)(NSError *error))fail{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:linkId forKey:@"fid"];
    
    NSURLSessionDataTask *task = [[VCInformationMoudle sharedHTTPSession] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if (succeed) {
            succeed(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
    return task;
}


/*流量上报
 e.appKey	appkey	 String
 e.resourceID	资源id	String
 e.playTime	播放时长，单位秒	Int
 e.accelSize	加速流量，单位字节	Long
 e.vcSize	Vc总的有效流量	Long
 e.vc		数组
 e.vc.host	Vc节点host	String
 e.vc.tSize	Vc节点总流量，单位字节	Long
 e.vc.vSize	Vc节点有效流量，单位字节	Long
 */
-(void)getReportWithResourceID:(NSString *)resourceID
                      playTime:(NSUInteger)playTime
                     accelSize:(NSUInteger)accelSize
                        vcSize:(NSUInteger)vcSize
                            vc:(NSArray *)vc
                       succeed:(void(^)(id data ))succeed
                          fail:(void(^)(NSError *error))fail{
    
    NSString  *urlstring = [NSString stringWithFormat:@"%@/v3/vcudp/flow",URL_string];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[VCCodeHelper AESDecryptIWithType:3 isMobileTV:NO] forKey:@"i"];
    [params setObject:[VCCodeHelper getReportWithResourceID:resourceID
                                                 playTime:playTime
                                                accelSize:accelSize
                                                   vcSize:vcSize
                                                       vc:vc] forKey:@"e"];
    
    [[VCInformationMoudle sharedHTTPSession] GET:urlstring parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dictsss = [VCCodeHelper decryptionPlayDataWithString:responseObject];
        if (succeed) {
            succeed(dictsss);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
        }
        
    }];
}

@end
